package com.tscnd.core.web.controller.login;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tscnd.core.utils.SecurityCode;
import com.tscnd.core.utils.SecurityImage;
import com.tscnd.core.utils.SecurityCode.SecurityCodeLevel;

/**
 * 登录 Controller
 * @author xiangtao
 *
 */
@Controller
@RequestMapping("login")
public class LoginController {
	/**
	 * 跳转到登录页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/tologin", method = RequestMethod.GET)
	public String toLogin(HttpServletRequest request,
			HttpServletResponse response) {
		timeoutVerify(request, response);
		return "/login";
	}
	
	
	/**
	 * 验证码
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/cjpge.jpg", method = RequestMethod.GET)
	public void toCjpge(HttpServletRequest request, HttpServletResponse response) {
		String url = request.getRequestURI();
		if (url.contains("cjpge.jpg")) {
			response.setContentType("image/jpeg");
			String validateCode = SecurityCode.getSecurityCode(4,
					SecurityCodeLevel.Simple, true);
			request.getSession().setAttribute("s_validateCode", validateCode);
			ByteArrayInputStream arrayInputStream = SecurityImage
					.getImageAsInputStream(validateCode);
			OutputStream outputStream = null;
			try {
				response.reset();
				outputStream = new BufferedOutputStream(
						response.getOutputStream());
				byte[] buffer = new byte[4096];
				int len = 0;
				// 写入输出流
				while ((len = arrayInputStream.read(buffer, 0, buffer.length)) != -1) {
					outputStream.write(buffer, 0, len);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					if (arrayInputStream != null) {
						arrayInputStream.close();
					}
					arrayInputStream = null;// 清空流
					if (outputStream != null) {
						outputStream.close();
					}
					outputStream = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
	
	/**
	 * 检查session 是否超时
	 * @param request
	 * @param response
	 */
	public void timeoutVerify(HttpServletRequest request, HttpServletResponse response){
		if("yes".equals(request.getParameter("timeout"))){
			PrintWriter out;
			try {
				response.setCharacterEncoding("utf-8");
				response.setContentType("text/html;charset=UTF-8");
				out = response.getWriter();
				StringBuilder builder = new StringBuilder();
				builder.append("<script type='text/javascript' charset='UTF-8'>");
				builder.append("alert(\"页面过期，请重新登录\");");
				builder.append("window.top.location.href='${ctx}/WEB-INF/view/login.jsp'");
				builder.append("</script>");
				out.print(builder.toString());
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
